home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / MTASK20.ZIP / MTASK.PRT < prev    next >
Encoding:
Text File  |  1990-03-15  |  18.8 KB  |  853 lines

  1.  
  2.  
  3. MTASK 2.0 by Wayne Conrad
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.                                 MTASK
  15.  
  16.  
  17. MTASK is a unit for Turbo Pascal 5.5, to allow a Turbo Pascal program
  18. to exhibit simple multi-tasking.  MTASK gives your program a
  19. non-preemptive, request driven multi-tasking capability.  I will
  20. explain what I mean by that later.
  21.  
  22. MTASK 2.0 was written and donated to the public domain by Wayne E.
  23. Conrad (me) in March of 1990.  I may be contact via my BBS,
  24.  
  25.      Pascalaholics Anonymous
  26.      (602) 484-9356
  27.      300/1200/2400 bps
  28.      24 hours/day
  29.  
  30. or by mail at my home:
  31.  
  32.      10 E Bell Road #1001
  33.      Phoenix, AZ  85022
  34.  
  35. I am interested in any modifications, bug reports, or comments you
  36. have.
  37.  
  38. If you modify this package, please keep my name and the name of any
  39. other programmers who've worked on it intact.  Give credit to yourself,
  40. too!  Please distribute the complete package, with documentation and
  41. demonstration programs included.
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                  -1-
  64.  
  65.  
  66.  
  67.  
  68.  
  69. MTASK 2.0 by Wayne Conrad
  70.  
  71.  
  72.  
  73. 1.1 INTRODUCTION
  74.  
  75.  
  76. MTASK allows your Turbo Pascal program to do simple multi-tasking.  I
  77. call MTASK's brand of multi-tasking "non-preemptive, request driven."
  78.  
  79. Preemptive means that the switch from one task to another can happen at
  80. almost any time.  Most preemptive systems have an interrupt driver
  81. hooked to a hardware timer, which causes a task switch every time the
  82. hardware timer goes off.  The advantage of this kind of multi-tasking
  83. is that your programs don't have to be written with multi-tasking in
  84. mind, and don't even have to know that its taking place.  Also, no
  85. program can hog the system for long, because the interrupt driver
  86. switches from one program to another fairly often.  Desqview and
  87. Double-DOS, and OS/2 are operating environments that do preemptive,
  88. interrupt-driven multi-tasking.  The disadvantage of this kind of
  89. multi-tasking is that it can be complex to write and difficult in the
  90. extreme to debug.  These difficulties are compounded in an MS-DOS
  91. environment because MS-DOS was not meant to be used in a multi-tasking
  92. environment.
  93.  
  94. On the other hand, non-preemptive multi-tasking only switches tasks at
  95. certain, well defined times.  There is no interrupt driver that forces
  96. task switches.  In the original Macintosh operating system, for example,
  97. task switches only occured when a task called the operating system.  The
  98. advantage of non-preemptive multi-tasking is that it is much simpler to
  99. write and debug than preemptive multi-tasking, because the system has
  100. total control of when task-switches occur.  The disadvantage to this
  101. form of multi-tasking is that a task must request a task switch often if
  102. the other tasks are to receive their chance to execute.  If a task does
  103. not request a task switch for a long time, the other tasks will appear
  104. to pause.  What's worse, if a task crashes, it won't be able to call the
  105. operating system to let the other tasks execute, so they'll all be hung
  106. too.
  107.  
  108. MTASK implements a very simple method of non-preemptive multi-tasking
  109. that I call "request driven."  Request driven means that task switches
  110. occur only when the current task calls MTASK and requests a switch.
  111. (The sole exception is that a task switch occurs when the current task
  112. terminates itself).  This is about the simplest form of multi-tasking
  113. that I can envision.  It is so simple that the entire MTASK unit
  114. compiles to only about 1400 bytes with stack checking and range
  115. checking turned off, or less if you don't use all of its procedures.
  116. This simplicity also made MTASK easy to write and debug.  MTASK was
  117. written in one day!
  118.  
  119.  
  120.  
  121. 1.2 WHAT ARE MTASK'S LIMITS?
  122.  
  123.  
  124. MTASK allows your program to set up multiple tasks within itself.
  125. These tasks will execute concurrently.  However, it does not effect
  126.  
  127.  
  128.  
  129.                                  -2-
  130.  
  131.  
  132.  
  133.  
  134.  
  135. MTASK 2.0 by Wayne Conrad
  136.  
  137.  
  138.  
  139. anything outside of your program.  It does not allow you to run
  140. multiple programs, multiple copies of COMMAND.COM, or anything else
  141. like that.  It simply allows your program to do several things
  142. concurrently without stumbling over itself.
  143.  
  144. As far as DOS is concerned, your program using MTASK is still just a
  145. simple program.  All of the gymnastics to keep track of multiple tasks
  146. are done by MTASK, withing your program, without the knowledge or
  147. consent of DOS or anything else outside of your program.  Because MTASK
  148. is so simple, it will coexist fine with any "real" multi-tasking DOS
  149. you have set up, such as DesqView or Double-DOS.  Whenever the DOS
  150. gives your program some time, your program will dole out that time to
  151. its tasks.
  152.  
  153. Your program must continue to execute for its tasks to execute.  If any
  154. task in your program exits to DOS for any reason, including a run-time
  155. error, all tasks stop executing.  If one of your tasks shells out by
  156. using Turbo's Exec function, then the other tasks in your program are
  157. suspended until control returns from the Exec function to your program.
  158.  
  159. MTASK must not be made into an overlay.  Any of the tasks it controls
  160. may be overlays, although that may be unwise.  You could end up loading
  161. an overlay from disk during each task switch!
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                  -3-
  196.  
  197.  
  198.  
  199.  
  200.  
  201. MTASK 2.0 by Wayne Conrad
  202.  
  203.  
  204.  
  205. 2.1 SUMMARY OF PROCEDURES AND FUNCTIONS
  206.  
  207.  
  208. To use MTASK, include it in your program's USES statements.  MTASK will
  209. initialize itself automatically, making your main program task #1.
  210. Your program can then use the following procedures and functions to
  211. create and control tasks:
  212.  
  213.  
  214.      create_task         Create another task
  215.  
  216.      terminate_task      Destroy a task
  217.  
  218.      switch_task         Switch to another task
  219.  
  220.      current_task_id     Return the task ID of the current task
  221.  
  222.      number_of_tasks     Return the current number of tasks
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                  -4-
  262.  
  263.  
  264.  
  265.  
  266.  
  267. MTASK 2.0 by Wayne Conrad
  268.  
  269.  
  270.  
  271. 2.1.1 PROCEDURE CREATE_TASK
  272.  
  273.  
  274. PROCEDURE create_task
  275.   (
  276.   task      : task_proc;
  277.   VAR param ;
  278.   stack_size: Word;
  279.   VAR id    : Word;
  280.   VAR result: Word
  281.   );
  282.  
  283.  
  284.      TASK is the procedure to make into a task.  It must match type
  285.      task_proc, having a single variable as its parameter.
  286.  
  287.      PARAM is the parameter to pass to new_task.  It may be a variable
  288.      of any type, so long its what the task expects.  For example, if
  289.      you pass a Word and the task expects a LongInt, the task will get
  290.      invalid data.
  291.  
  292.      STACK_SIZE is the size of the new task's stack.  A stack will be
  293.      allocated from the heap.  The minimum stack size is 512 bytes, but
  294.      most tasks will need more.
  295.  
  296.      ID is set to the task ID of the newly created task.  If the task
  297.      is not created because of an error, then id is not set.
  298.  
  299.      RESULT is the result code, which is set to one of these values:
  300.  
  301.           0                   No error, task created ok
  302.  
  303.           heap_full           Unable to allocate heap for the task's
  304.                               stack
  305.  
  306.           too_many_tasks      Maximum number of tasks are already
  307.                               running
  308.  
  309.  
  310. The new task is created and added to the end of the task list.  The new
  311. task will be executed when the task before it calls switch_task.
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                  -5-
  328.  
  329.  
  330.  
  331.  
  332.  
  333. MTASK 2.0 by Wayne Conrad
  334.  
  335.  
  336.  
  337. 2.1.2 PROCEDURE TERMINATE_TASK
  338.  
  339.  
  340. PROCEDURE terminate_task (id: Word; VAR result: Word);
  341.  
  342.  
  343.      ID is the task id of the task you want to terminate.  If ID = 0,
  344.      then the current task will be terminated.
  345.  
  346.      RESULT is the result code, which is set to one of these values.
  347.  
  348.           0                   No error, task deleted ok
  349.  
  350.           invalid_task_id     There is no task with that ID number
  351.  
  352.  
  353. The designated task will be removed from the task list.  If its stack
  354. was allocated from the heap, it is returned to the heap.
  355.  
  356. If the terminated task is the current task and there is another task in
  357. the task list, a task switch occurs.  On the other hand, if the
  358. terminated task is the current task and there are no other tasks in the
  359. task list, then the program exits to DOS.
  360.  
  361. A task may terminate itself by returning from its main procedure.  For
  362. example, when this task is executed, it will immediately display a
  363. message and then terminate itself.
  364.  
  365.  
  366.      PROCEDURE task (VAR param);
  367.      BEGIN
  368.        Writeln ('We just started, but already we're terminating')
  369.      END;
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.                                  -6-
  394.  
  395.  
  396.  
  397.  
  398.  
  399. MTASK 2.0 by Wayne Conrad
  400.  
  401.  
  402.  
  403. 2.1.3 PROCEDURE switch_task
  404.  
  405.  
  406.      PROCEDURE switch_task;
  407.  
  408.  
  409. This procedure causes an immediate switch to the next task in the task
  410. list.  The task list is always scanned as a circular list.  For
  411. example, if there are three tasks in the list -- task 1, task 2, and
  412. task 3 -- then they will be executed in this order:
  413.  
  414.  
  415.      1, 2, 3, 1, 2, 3, 1, 2, 3 . . .
  416.  
  417.  
  418. If the current task is the only task, then no task switch occurs.
  419.  
  420. The stack pointer is switched to its position in the new task's stack.
  421. If the new task has just been created, then its main procedure will be
  422. executed from the beginning.  On the other hand, if the new task had
  423. put itself to sleep by asking for a task switch, then control will
  424. return to the point where it called switch_task.
  425.  
  426.  
  427.  
  428. 2.1.4 FUNCTION CURRENT_TASK_ID
  429.  
  430.  
  431.      FUNCTION current_task_id: Word;
  432.  
  433.  
  434. This function returns the task ID number of the currently executing
  435. task.  When calling an MTASK procedure to do something to a task, the
  436. task ID number is always used to identify the task.
  437.  
  438. A task is assigned its ID number when it is created.  A task's ID
  439. number belongs to it as long as that task exists, and will not be
  440. changed or reassigned until the task terminates.  Even after the task
  441. terminates, Mtask will avoid re-assigning its ID for as long as
  442. possible.  Since there are 65535 possible task ID's, this could be a
  443. very long time indeed.
  444.  
  445.  
  446.  
  447. 2.1.5 FUNCTION NUMBER_OF_TASKS
  448.  
  449.  
  450.      FUNCTION number_of_tasks: Word;
  451.  
  452.  
  453. This function returns the number of tasks in the task list.  There will
  454. always be at least one task.
  455.  
  456.  
  457.  
  458.  
  459.                                  -7-
  460.  
  461.  
  462.  
  463.  
  464.  
  465. MTASK 2.0 by Wayne Conrad
  466.  
  467.  
  468.  
  469. 3.1 TRICKS AND TRAPS
  470.  
  471.  
  472. This section focuses on some of the tricks and traps of programming in
  473. this multi-tasking environment.  Like all multi-tasking environments,
  474. strange things can happen.  You'll learn how to watch for problems with
  475. shared data, and crunched parameters.
  476.  
  477. I will only give a few examples of the problems that can occur in
  478. multi-tasking environments.  There are other problems that can occur
  479. when using MTASK, although the problems are less numerous and simpler
  480. to solve than when using a preemptive multi-tasking system.  This
  481. section should help you to begin thinking like a real-time programmer,
  482. giving you an idea of the kinds of problems to watch for.  For a real
  483. education on concurrent programming, head to your library or book store
  484. and look for a book on operating systems.
  485.  
  486.  
  487.  
  488. 3.1.1 PASSING PARAMETERS TO TASKS
  489.  
  490.  
  491. When you create a task, you can pass a parameter to it.  For example, a
  492. BBS program needs to tell a task which node it is, so that the task
  493. knows which serial port to use for i/o.  The parameter you pass is
  494. "untyped," meaning that it can be any type of variable.  You must be
  495. familiar with how Turbo handles untyped variables.
  496.  
  497. The sample program TEST18S shows how to pass a word variable to a
  498. task.  You can pass any kind of variable, including records, arrays,
  499. and even files.
  500.  
  501. One thing to remember is that when you pass an untyped parameter to a
  502. task, you are actually passing the address of the parameter, not the
  503. parameter itself.  Therefore, if you pass the task a paramater and then
  504. modify the parameter, the task may see the new value instead of the old
  505. value.  It will all depend upon where task switches occur.
  506.  
  507. As a general rule, parameters you pass to a task should be global
  508. variables or typed constants.  Global variables and typed constants are
  509. both in the data segment.  Local variables are declared on the current
  510. task's stack, and cannot be assured of existing for very long.  If you
  511. a procedure passes one of its local variables to a task that it's
  512. creating, and then the procedure returns, that local variable is
  513. "thrown away" and its space can be reused by other procedures.  That
  514. would cause the value of the parameter you passed to the task to change
  515. unpredictably.
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.                                  -8-
  526.  
  527.  
  528.  
  529.  
  530.  
  531. MTASK 2.0 by Wayne Conrad
  532.  
  533.  
  534.  
  535. 3.1.2 SHARED DATA
  536.  
  537.  
  538. Problems can occur when two or more tasks are using the same global
  539. variables.  If two or more tasks have access to the same variable, you
  540. need to consider carefully what will happen if two tasks access the
  541. variable concurrently.  This pseudo-code example shows two tasks.
  542. task_a is computing the sum of an array of Reals.  Task_b is clearing
  543. the values in the array.
  544.  
  545.  
  546.      CONST
  547.        data_size = 1000;
  548.      VAR
  549.        data: ARRAY [1.03/15/90ta_size] OF Real;
  550.  
  551.  
  552.      PROCEDURE task_a;
  553.         .
  554.         .
  555.         .
  556.      sum := 0.0;
  557.      FOR i := 1 TO data_size DO
  558.        BEGIN
  559.        sum := sum + data [i];
  560.        switch_task;
  561.        END;
  562.         .
  563.         .
  564.         .
  565.      END;
  566.  
  567.  
  568.      PROCEDURE task_b;
  569.         .
  570.         .
  571.         .
  572.      FOR i := data_size DOWNTO 1 DO
  573.        BEGIN
  574.        data [i] := 0.0;
  575.        switch_task;
  576.        END;
  577.         .
  578.         .
  579.         .
  580.      END;
  581.  
  582.  
  583. Do you see what happens if task_a is computing the average at the same
  584. time task_b is clearing the array?  The average will end up being
  585. incorrect, because the data being averaged is being changed while the
  586. average is being computed.  Obviously, this example is contrived.
  587. Nobody in their right mind would call switch_task inside those loops.
  588.  
  589.  
  590.  
  591.                                  -9-
  592.  
  593.  
  594.  
  595.  
  596.  
  597. MTASK 2.0 by Wayne Conrad
  598.  
  599.  
  600.  
  601. That causes many more context switches than are necessary.
  602.  
  603. One way to avoid the problem in this particular example is not to call
  604. switch_task inside either of the loops.  Then you could be sure that an
  605. average would not take place while you were clearing the array, and
  606. array clearing would not take place during an average.
  607.  
  608. You cannot always avoid calling switch_task, however.  Suppose that
  609. floating point addition on your computer was so slow that it took many
  610. seconds to compute the average.  You may have other tasks that cannot
  611. afford to be denied CPU time for more than a fraction of a second.
  612. What do you do?
  613.  
  614. The solution here is to create a flag that indicates when a task is
  615. using the data array.  When one task is using the data array the flag
  616. will be set to True, indicating that no other task should access it.
  617.  
  618.  
  619.      CONST
  620.        flag: Boolean = False;
  621.  
  622.  
  623.      PROCEDURE task_a;
  624.         .
  625.         .
  626.         .
  627.      WHILE flag DO
  628.        switch_task;
  629.      flag := True;
  630.      sum := 0.0;
  631.      FOR i := 1 TO data_size DO
  632.        BEGIN
  633.        sum := sum + data [i];
  634.        switch_task;
  635.        END;
  636.      flag := False;
  637.         .
  638.         .
  639.         .
  640.      END;
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.                                  -10-
  658.  
  659.  
  660.  
  661.  
  662.  
  663. MTASK 2.0 by Wayne Conrad
  664.  
  665.  
  666.  
  667.      PROCEDURE task_b;
  668.         .
  669.         .
  670.         .
  671.      WHILE flag DO
  672.        switch_task;
  673.      flag := True;
  674.      FOR i := data_size DOWNTO 1 DO
  675.        BEGIN
  676.        data [i] := 0.0;
  677.        switch_task;
  678.        END;
  679.      flag := True;
  680.         .
  681.         .
  682.         .
  683.      END;
  684.  
  685.  
  686. Do you see what's going on here?  Before task_a does an average, it
  687. checks the flag to see whether someone else is messing with the data
  688. array.  If someone is, then it waits until the data structure is
  689. available, sets the flag to indicate that it now "owns" the data array,
  690. and proceeds to compute the average.  When the average is finished,
  691. task_a resets the flag, to allow any other task which is waiting for
  692. the data array to have access.  task_b is doing exactly the same thing.
  693.  
  694. Now both tasks can go on calling switch_task even while messing with
  695. the data array, without concern that some other task will access the
  696. data array at the same time.  This technique will work for any number
  697. of tasks.
  698.  
  699.  
  700.  
  701. 3.1.3 WHEN TO SWITCH TASKS?
  702.  
  703.  
  704. Obviously, the examples in 3.1.2 switch tasks far too often.  The
  705. program will spend more time bouncing from one task to another than it
  706. will doing anything useful!  If your loop is too time consuming to
  707. leave out task switches, and switching tasks during every iteration of
  708. the loop is too often, try something like this:
  709.  
  710.  
  711.      FOR i := 1 TO 10000 DO
  712.        BEGIN
  713.        IF i MOD 100 = 0 THEN
  714.          switch_task;
  715.        do_something_useful;
  716.        END;
  717.  
  718.  
  719. This will switch tasks every hundreth iteration of the loop.
  720.  
  721.  
  722.  
  723.                                  -11-
  724.  
  725.  
  726.  
  727.  
  728.  
  729. MTASK 2.0 by Wayne Conrad
  730.  
  731.  
  732.  
  733. If your program is going to do something that takes a while, like disk
  734. i/o, it should probably switch tasks before doing so to let the other
  735. tasks get some time before the long delay occurs.  In fact, if you are
  736. doing several lengthy disk operations in a row, call switch_task before
  737. every one.
  738.  
  739.  
  740.      Assign (inf, 'INPUT03/15/90T');
  741.      switch_task;
  742.      Reset (inf);
  743.      Assign (outf, 'OUTPUT03/15/90T');
  744.      switch_task;
  745.      Rewrite (outf);
  746.  
  747.  
  748. Many programs have to wait for input at some point.  Input loops are a
  749. perfect place to switch tasks.  In fact, any time a task cannot proceed
  750. because its input is not ready, or for any other reason, it should
  751. switch tasks.
  752.  
  753.  
  754.      WHILE NOT KeyPressed DO
  755.        switch_task;
  756.      ch := ReadKey;
  757.  
  758.  
  759. It is a matter of judgement where task switches should occur.  It will
  760. depend upon the program and circumstances around each operation.
  761.  
  762.  
  763.  
  764. 4.1 REVISION HISTORY
  765.  
  766.  
  767. Version 1.0, MTASK10.ARC.
  768.  
  769.      Original release by Wayne E. Conrad
  770.  
  771. Version 1.1, MTASK11.ARC.
  772.  
  773.      Minor changes to documentation, including using spaces instead of
  774.      tabs.  ARC file now includes the original documentation in
  775.      Multi-Edit format, as well as the printable file.
  776.  
  777. Version 2.0, MTASK20.ZIP.
  778.  
  779.      When a task is terminated, Mtask tries to not assign that task's
  780.      id to a new task for as long as possible.  In order to accomplish
  781.      this, task id's are now words (1..65535).
  782.  
  783.      The create_task procedure was often returning bogus error numbers
  784.      when in fact no error had occured.  Fixed.
  785.  
  786.  
  787.  
  788.  
  789.                                  -12-
  790.  
  791.  
  792.  
  793.  
  794.  
  795. MTASK 2.0 by Wayne Conrad
  796.  
  797.  
  798.  
  799.      The get_task_info procedure is gone, in preparation for a change
  800.      in the way task information is stored.  If you need this
  801.      procedure, you should be able to copy it out of version 1.1
  802.      without any problems.
  803.  
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815.  
  816.  
  817.  
  818.  
  819.  
  820.  
  821.  
  822.  
  823.  
  824.  
  825.  
  826.  
  827.  
  828.  
  829.  
  830.  
  831.  
  832.  
  833.  
  834.  
  835.  
  836.  
  837.  
  838.  
  839.  
  840.  
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.                                  -13-
  853.